package com.activity;

import android.content.Intent;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.tools.CheckPhone;

import com.tools.SendCode;
import com.tools.SomeMonitorEditText;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;



public class Register1Activity extends AppCompatActivity {

    private TextView 标题;
    private ImageButton 后退按钮;
    private EditText 编辑手机号;
    private EditText 编辑验证码;
    private TextView 发送验证码;
    private Button 下一步按钮;

    //传递的手机号码
    String phone = "";

    //验证码设置
    String word = "";
    char[] checkCode ={'0','1','2','3','4','5','6','7','8','9'};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone);

        标题 = (TextView) findViewById(R.id.标题);
        后退按钮 = (ImageButton) findViewById(R.id.后退按钮);
        编辑手机号 = (EditText) findViewById(R.id.编辑手机号);
        编辑验证码 = (EditText) findViewById(R.id.编辑验证码);
        发送验证码 = (TextView) findViewById(R.id.发送验证码);
        下一步按钮 = (Button) findViewById(R.id.下一步按钮);

        标题.setText("注册验证");

        //没写验证码无法提交
        new SomeMonitorEditText().SetMonitorEditText(下一步按钮,编辑验证码);

        发送验证码.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String pNo = 编辑手机号.getText().toString().trim();
                if(pNo.length()==0||pNo.equals("")){
                    Toast.makeText(Register1Activity.this,"手机号码输入为空！",Toast.LENGTH_SHORT).show();
                }else if(!CheckPhone.isMobileNO(pNo)){
                    Toast.makeText(Register1Activity.this,"手机号码格式有误！",Toast.LENGTH_SHORT).show();
                    编辑手机号.setText(null);
                }else{
                    String path = "http://101.200.40.47:8080/stu/user/checkPhone?phone="+pNo;
                    new Verification().execute(path);
                }
            }
        });

        后退按钮.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(Register1Activity.this, LoginActivity.class);
                startActivity(intent);
                Register1Activity.this.finish();
            }
        });

        下一步按钮.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code = 编辑验证码.getText().toString().trim();
                if(code.equals(word)&&!word.equals("")){
                    Toast.makeText(Register1Activity.this, "验证成功！", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    intent.putExtra("phone",phone);
                    intent.setClass(Register1Activity.this, Register2Activity.class);
                    startActivity(intent);
                    Register1Activity.this.finish();
                }else if(word.equals("")){
                    Toast.makeText(Register1Activity.this, "未获取验证码！", Toast.LENGTH_SHORT).show();
                    编辑验证码.setText(null);
                } else{
                    Toast.makeText(Register1Activity.this, "验证码输入有误!", Toast.LENGTH_SHORT).show();
                    编辑验证码.setText(null);
                }

            }
        });

    }
    //判断手机号码
    private class Verification extends AsyncTask {

        @Override
        protected Object doInBackground(Object[] params) {

            String path = params[0].toString();

            try {
                //以get方式提交整个路径
                URL url = new URL(path);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");//这个应该都知道是干嘛的，默认是GET  
                conn.setConnectTimeout(5000);

                if (conn.getResponseCode() == 200) {
                    InputStream is = conn.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    String str = br.readLine();
                    return str;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            String info = o.toString().trim();
            if(info.equals("1")){
                Toast.makeText(Register1Activity.this, "该手机号码已被注册！", Toast.LENGTH_SHORT).show();
                编辑手机号.setText(null);
            }else if(info.equals("0")){
                phone = 编辑手机号.getText().toString().trim();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for(int i=0;i<4;i++){
                            word+=checkCode[(int)(checkCode.length*Math.random())];
                        }
                        SendCode sc = new SendCode();
                        String json = sc.bowlingJson(word,phone);
                        try{
                            String response = sc.post("https://open.ucpaas.com/ol/sms/sendsms",json);
                            Looper.prepare();
                            Toast.makeText(Register1Activity.this, "验证码已发送", Toast.LENGTH_SHORT).show();
                            Looper.loop();

                        }catch(IOException e) {
                            Looper.prepare();
                            Toast.makeText(Register1Activity.this, "验证码发送失败", Toast.LENGTH_SHORT).show();
                            Looper.loop();
                            e.printStackTrace();
                        }
                    }
                }).start();
            }

        }
    }

    long mExitTime;       //实现“再按一次退出”的记录时间变量
    @Override //再按一次退出程序
    public void onBackPressed() {
        if (System.currentTimeMillis() - mExitTime < 2000) {
            super.onBackPressed();
        } else {
            mExitTime = System.currentTimeMillis();
            Toast.makeText(Register1Activity.this,"再按一次退出应用",Toast.LENGTH_SHORT).show();
        }
    }

}






